import { getBehaviourClassByName, getGameObjectById } from "../engine";
import { Behaviour } from "../engine/Behaviour";
import { BitmapRenderer } from "../engine/BitmapRenderer";
import { Transform } from "../engine/Transform";
import { BackGroundBinding } from "./BackGroundBinding";
import { ButtonBinding } from "./ButtonBinding";
import { Camera } from "./Camera";
import { GameObjectAcitivable } from "./GameObjectAcitivable";
import { PhotoWalk } from "./PhotoWalk";
import { Scene1Binding } from "./Scene1Binding";
import { Sound } from "./sound";


function sleep(delay){
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay){
        continue;
    }
}


export class ClickSceneChange extends Behaviour {

    
    onStart(): void{
        
        this.gameObject.onMove=(event)=>{
            const parent = this.gameObject.engine.rootGameObject;
            const engine=this.gameObject.engine;
            const index = parent.children.length;
            if(this.gameObject.id==="start"||this.gameObject.id==="explain"||this.gameObject.id==="about"||this.gameObject.id==="replay"
            ||this.gameObject.id==="exit"||this.gameObject.id==="backmain"||this.gameObject.id==="choosestage"||this.gameObject.id==="setting"
            ||this.gameObject.id==="continuegame"||this.gameObject.id==="back"
            ||this.gameObject.id==="stop"||this.gameObject.id==="stage1"||this.gameObject.id==="stage2"||this.gameObject.id==="stage3"
            ||this.gameObject.id==="stage4"||this.gameObject.id==="stage5"||this.gameObject.id==="stage6"||this.gameObject.id==="stage7"
            ||this.gameObject.id==="stage8"){
                this.gameObject.getBehaviour(ButtonBinding).imageSource='./assets/images/'+this.gameObject.id+'-highlight.png'
            }
            if(this.gameObject.parent.id==='SceneMainwithTitle_real'){
                getGameObjectById('start').getBehaviour(ButtonBinding).imageSource='./assets/images/start.png'
                getGameObjectById('explain').getBehaviour(ButtonBinding).imageSource='./assets/images/explain.png'
                getGameObjectById('about').getBehaviour(ButtonBinding).imageSource='./assets/images/about.png'
                getGameObjectById('exit').getBehaviour(ButtonBinding).imageSource='./assets/images/exit.png'
                getGameObjectById('setting').getBehaviour(ButtonBinding).imageSource='./assets/images/setting.png'
            }
            for(let i=2;i<=8;i++){
            if(this.gameObject.id==='reStartCurrentScene'+i+'Button'){
                this.gameObject.getBehaviour(ButtonBinding).imageSource='./assets/images/replay-highlight.png'
            }
            if(this.gameObject.parent.id==='SceneMainwithTitle'+i){
                getGameObjectById('backmain').getBehaviour(ButtonBinding).imageSource='./assets/images/backmain.png'
                getGameObjectById('exit').getBehaviour(ButtonBinding).imageSource='./assets/images/exit.png'
                getGameObjectById('reStartCurrentScene'+i+'Button').getBehaviour(ButtonBinding).imageSource='./assets/images/replay.png'
            }}
            if(this.gameObject.parent.id==="ChooseMenu_BackGround"){
                getGameObjectById('back').getBehaviour(ButtonBinding).imageSource='./assets/images/back.png'
                getGameObjectById('stage1').getBehaviour(ButtonBinding).imageSource='./assets/images/stage1.png'
                getGameObjectById('stage2').getBehaviour(ButtonBinding).imageSource='./assets/images/stage2.png'
                getGameObjectById('stage3').getBehaviour(ButtonBinding).imageSource='./assets/images/stage3.png'
                getGameObjectById('stage4').getBehaviour(ButtonBinding).imageSource='./assets/images/stage4.png'
                getGameObjectById('stage5').getBehaviour(ButtonBinding).imageSource='./assets/images/stage5.png'
                getGameObjectById('stage6').getBehaviour(ButtonBinding).imageSource='./assets/images/stage6.png'
                getGameObjectById('stage7').getBehaviour(ButtonBinding).imageSource='./assets/images/stage7.png'
                getGameObjectById('stage8').getBehaviour(ButtonBinding).imageSource='./assets/images/stage8.png'                
            }
            if(this.gameObject.parent.id==="Scene1_BackGround"||this.gameObject.parent.id==="Scene2_BackGround"||this.gameObject.parent.id==="Scene3_BackGround"
            ||this.gameObject.parent.id==="Scene4_BackGround"||this.gameObject.parent.id==="Scene5_BackGround"||this.gameObject.parent.id==="Scene6_BackGround"
            ||this.gameObject.parent.id==="Scene7_1_BackGround"||this.gameObject.parent.id==="Scene7_2_BackGround"||this.gameObject.parent.id==="Scene8_BackGround"){
                getGameObjectById('stop').getBehaviour(ButtonBinding).imageSource='./assets/images/stop.png'
            }
            if(this.gameObject.parent.id==="SettingBackGround"){
                getGameObjectById('back').getBehaviour(ButtonBinding).imageSource='./assets/images/back.png'
            }
            if(this.gameObject.parent.id==="ExplainBackGround"){
                getGameObjectById('back').getBehaviour(ButtonBinding).imageSource='./assets/images/back.png'
            }
            if(this.gameObject.parent.id==="AboutBackGround"){
                getGameObjectById('back').getBehaviour(ButtonBinding).imageSource='./assets/images/back.png'
            }
            if(this.gameObject.parent.id==="PauseBackGround"){
                getGameObjectById('continuegame').getBehaviour(ButtonBinding).imageSource='./assets/images/continuegame.png'
                getGameObjectById('backmain').getBehaviour(ButtonBinding).imageSource='./assets/images/backmain.png'
                getGameObjectById('choosestage').getBehaviour(ButtonBinding).imageSource='./assets/images/choosestage.png'
            }
            if(this.gameObject.parent.id==="SuccessBackGround"){
                getGameObjectById('backmain').getBehaviour(ButtonBinding).imageSource='./assets/images/backmain.png'
            }
        }
        
        this.gameObject.onClick = (event) =>{
            if (this.gameObject.engine.mode === "play") {
                const parent = this.gameObject.engine.rootGameObject;
                const index = parent.children.length;

                const sound=getBehaviourClassByName('Sound')
                //主菜单
                if(this.gameObject.id === "start"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                    this.gameObject.children[0].getBehaviour(Sound).play();
                    }
                    sleep(1000);
                    parent.removeAllChildren();
                    const CHOOSEMENU = this.gameObject.engine.createPrefab('./assets/scenes/chooseMenu.yaml');
                    parent.addChild(CHOOSEMENU);
                }
                if(this.gameObject.id === "explain"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const EXPLAIN = this.gameObject.engine.createPrefab('./assets/scenes/explain.yaml');
                    parent.addChild(EXPLAIN);
                }
                if(this.gameObject.id === "about"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const ABOUT = this.gameObject.engine.createPrefab('./assets/scenes/about.yaml');
                    parent.addChild(ABOUT);
                }
                if(this.gameObject.id === "setting"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SETTING = this.gameObject.engine.createPrefab('./assets/scenes/setting.yaml');
                    parent.addChild(SETTING);
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        const closesound=getGameObjectById('CloseSound')
                        closesound.children[0].getBehaviour(GameObjectAcitivable).IsActive=true;
                        const opensound=getGameObjectById('OpenSound')
                        opensound.children[0].getBehaviour(GameObjectAcitivable).IsActive=false;
                    }
                    else if(sound.isOpen===false){
                        const closesound=getGameObjectById('CloseSound')
                        closesound.children[0].getBehaviour(GameObjectAcitivable).IsActive=false;
                        const opensound=getGameObjectById('OpenSound')
                        opensound.children[0].getBehaviour(GameObjectAcitivable).IsActive=true;
                    }
                }

                if(this.gameObject.id === "exit"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        window.close();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const ABOUT = this.gameObject.engine.createPrefab('./assets/scenes/about.yaml');
                    parent.addChild(ABOUT);
                }

                //所有返回
                if(this.gameObject.id === "back"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const MENU = this.gameObject.engine.createPrefab('./assets/scenes/menu.yaml');
                    parent.addChild(MENU);
                }

                
                //设置界面CloseSound按钮
                if(this.gameObject.id === "CloseSound"){
                    //控制音量开关！！！！！！！！！！！！！！！！！！！！！！！！
                    //将所有预制体里的Sound组件的isOpen属性设置为false
                    const SoundBehaviour=getBehaviourClassByName('Sound')
                    SoundBehaviour.isOpen=false 
                    this.gameObject.children[0].getBehaviour(GameObjectAcitivable).IsActive=false;
                    const opensound=getGameObjectById('OpenSound')
                    opensound.children[0].getBehaviour(GameObjectAcitivable).IsActive=true;
                    console.log("关闭声音"+SoundBehaviour.isOpen);
                    
                }
                //设置界面OpenSound按钮
                if(this.gameObject.id === "OpenSound"){
                    //控制音量开关！！！！！！！！！！！！！！！！！！！！！！！！
                    //将所有预制体里的Sound组件的isOpen属性设置为true
                    const SoundBehaviour=getBehaviourClassByName('Sound')
                    SoundBehaviour.isOpen=true
                    this.gameObject.children[0].getBehaviour(GameObjectAcitivable).IsActive=false;
                    const opensound=getGameObjectById('CloseSound')
                    opensound.children[0].getBehaviour(GameObjectAcitivable).IsActive=true;
                    console.log("开启声音"+SoundBehaviour.isOpen);
                }

                //选关
                if(this.gameObject.id === "stage1"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                    }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE1 = this.gameObject.engine.createPrefab('./assets/scenes/dialogscene1.yaml');
                    parent.addChild(SCENE1);
                    
                }
                if(this.gameObject.id === "stage2"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE1 = this.gameObject.engine.createPrefab('./assets/scenes/dialogscene2.yaml');
                    parent.addChild(SCENE1);
                }
                if(this.gameObject.id === "stage3"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE1 = this.gameObject.engine.createPrefab('./assets/scenes/dialogscene3.yaml');
                    parent.addChild(SCENE1);
                }
                if(this.gameObject.id === "stage4"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE1 = this.gameObject.engine.createPrefab('./assets/scenes/dialogscene4.yaml');
                    parent.addChild(SCENE1);
                }
                if(this.gameObject.id === "stage5"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE1 = this.gameObject.engine.createPrefab('./assets/scenes/dialogscene5.yaml');
                    parent.addChild(SCENE1);
                }
                if(this.gameObject.id === "stage6"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE1 = this.gameObject.engine.createPrefab('./assets/scenes/dialogscene6.yaml');
                    parent.addChild(SCENE1);
                }
                if(this.gameObject.id === "stage7"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE1 = this.gameObject.engine.createPrefab('./assets/scenes/dialogscene7.yaml');
                    parent.addChild(SCENE1);
                }
                if(this.gameObject.id === "stage8"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE1 = this.gameObject.engine.createPrefab('./assets/scenes/dialogscene8.yaml');
                    parent.addChild(SCENE1);
                }


                //最后成功界面
                if(this.gameObject.id === "backmain"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const BACKMAIN = this.gameObject.engine.createPrefab('./assets/scenes/menu.yaml');
                    parent.addChild(BACKMAIN);
                }

                
                //暂停界面（没有真的暂停）
                if(this.gameObject.id === "backmain"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const BACKMAIN = this.gameObject.engine.createPrefab('./assets/scenes/menu.yaml');
                    parent.addChild(BACKMAIN);
                    
                }
                if(this.gameObject.id === "choosestage"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const CHOOSESTAGE = this.gameObject.engine.createPrefab('./assets/scenes/chooseMenu.yaml');
                    parent.addChild(CHOOSESTAGE);
                }


                //所有暂停
                if(this.gameObject.id === "stop"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    const PAUSE = this.gameObject.engine.createPrefab('./assets/scenes/pause.yaml');
                    PAUSE.id='pause'
                    parent.addChild(PAUSE);
                }
                
                //继续游戏
                if(this.gameObject.id === "continuegame"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeChild(parent.children[1])
                }

                //場景2失敗頁面（重玩本关   退出）
                if(this.gameObject.id === "reStartCurrentScene2Button"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE2 = this.gameObject.engine.createPrefab('./assets/scenes/scene2.yaml');
                    parent.addChild(SCENE2);
                    const scene2=getGameObjectById('Scene2')
                    const BGMsound=getBehaviourClassByName('Sound')
                    if(scene2){
                        if(BGMsound.isOpen===true || BGMsound.isOpen===undefined){
                            scene2.getBehaviour(Sound).play();
                        }
                    }
                }
                 
                //場景3失敗頁面（重玩本关   退出）
                if(this.gameObject.id === "reStartCurrentScene3Button"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE3 = this.gameObject.engine.createPrefab('./assets/scenes/scene3.yaml');
                    parent.addChild(SCENE3);
                    const scene3=getGameObjectById('Scene3')
                    const BGMsound=getBehaviourClassByName('Sound')
                    if(scene3){
                        if(BGMsound.isOpen===true || BGMsound.isOpen===undefined){
                            scene3.getBehaviour(Sound).play();
                        }
                    }
                }

                //場景4失敗頁面（重玩本关   退出）
                if(this.gameObject.id === "reStartCurrentScene4Button"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.children.splice(0, index);
                    const SCENE4 = this.gameObject.engine.createPrefab('./assets/scenes/scene4.yaml');
                    parent.addChild(SCENE4);
                    const scene4=getGameObjectById('Scene4')
                    const BGMsound=getBehaviourClassByName('Sound')
                    if(scene4){
                        if(BGMsound.isOpen===true || BGMsound.isOpen===undefined){
                            scene4.getBehaviour(Sound).play();
                        }
                    }
                }

                 //場景5失敗頁面（重玩本关   退出）
                 if(this.gameObject.id === "reStartCurrentScene5Button"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE5 = this.gameObject.engine.createPrefab('./assets/scenes/scene5.yaml');
                    parent.addChild(SCENE5);
                    const scene5=getGameObjectById('Scene5')
                    const BGMsound=getBehaviourClassByName('Sound')
                    if(scene5){
                        if(BGMsound.isOpen===true || BGMsound.isOpen===undefined){
                            scene5.getBehaviour(Sound).play();
                        }
                    }
                }



                //場景6失敗頁面（重玩本关   退出）
                if(this.gameObject.id === "reStartCurrentScene6Button"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.children.splice(0, index);
                    const SCENE6 = this.gameObject.engine.createPrefab('./assets/scenes/scene6.yaml');
                    parent.addChild(SCENE6);
                    const scene6=getGameObjectById('Scene6')
                    const BGMsound=getBehaviourClassByName('Sound')
                    if(scene6){
                        if(BGMsound.isOpen===true || BGMsound.isOpen===undefined){
                            scene6.getBehaviour(Sound).play();
                        }
                    }
                }

                 //場景7失敗頁面（重玩本关   退出）
                 if(this.gameObject.id === "reStartCurrentScene7Button"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.removeAllChildren();
                    const SCENE7 = this.gameObject.engine.createPrefab('./assets/scenes/scene7_1.yaml');
                    parent.addChild(SCENE7);
                    const scene7=getGameObjectById('Scene7')
                    const BGMsound=getBehaviourClassByName('Sound')
                    if(scene7){
                        if(BGMsound.isOpen===true || BGMsound.isOpen===undefined){
                            scene7.getBehaviour(Sound).play();
                        }
                    }
                }

                //場景8失败頁面（重玩本关   退出）
                if(this.gameObject.id === "reStartCurrentScene8Button"){
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        this.gameObject.children[0].getBehaviour(Sound).play();
                        }
                    sleep(1000);
                    parent.children.splice(0, index);
                    const SCENE8 = this.gameObject.engine.createPrefab('./assets/scenes/scene8.yaml');
                    parent.addChild(SCENE8);
                    const scene8=getGameObjectById('Scene8')
                    const BGMsound=getBehaviourClassByName('Sound')
                    if(scene8){
                        if(BGMsound.isOpen===true || BGMsound.isOpen===undefined){
                            scene8.getBehaviour(Sound).play();
                        }
                    }
                }
            }
        }
    }
//     onTick(duringTime: number): void {
        
        
// }
}